//  this library.
import 'package:flutter/material.dart';
import 'package:hktk_app/ienum/question/first_type_enum.dart';
import 'package:hktk_app/util/font_util.dart';
import 'package:hktk_app/util/my_util.dart';
import 'package:hktk_app/util/sz_util.dart';
import 'package:hktk_app/util/view_util.dart';
import 'package:hktk_app/widget/question/stem_rich_text_widget.dart';

/// 做题页面答案组件
class StemAnswerWidget extends StatefulWidget {
  /// 答案
  final List<String> ans;

  /// 已选中
  final List<String> sel;

  /// 试题题型
  final FirstTypeEnum qType;

  /// 选中是否正确
  final bool suc;

  /// 是否显示你选择的
  final bool showSel;

  /// 做题次数
  final int doingNumber;

  /// 错题次数
  final int errorNumber;

  const StemAnswerWidget({
    super.key,
    required this.ans,
    required this.sel,
    required this.qType,
    required this.suc,
    required this.showSel,
    required this.doingNumber,
    required this.errorNumber,
  });

  @override
  State<StemAnswerWidget> createState() => _StemAnswerWidgetState();
}

class _StemAnswerWidgetState extends State<StemAnswerWidget> {
  /// 是否显示全站数据
  late bool _showQzsj;

  @override
  void initState() {
    super.initState();
    debugPrint("stemAnswerWidget 组件 -> initState 方法执行 ===> ");
    debugPrint("");
    _showQzsj = false;
  }

  @override
  Widget build(BuildContext context) {
    debugPrint("stemAnswerWidget 组件 -> build 方法执行 ===> ");
    debugPrint("");
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20, vertical: 18),
      decoration: BoxDecoration(
        color: szUtilComm.szInfo.yjms ? colorBlack13 : colorWhite2,
        borderRadius: BorderRadius.all(Radius.circular(8)),
      ),
      child: _content(),
    );
  }

  /// 内容显示方法
  _content() {
    // return Row(
    //   children: [
    //     Expanded(
    //       child: Column(
    //         crossAxisAlignment: CrossAxisAlignment.start,
    //         children: [
    //           _showAns(),
    //           if (_showQzsj) hiSpace(height: 30),
    //           if (_showQzsj) _showQzsjItem(),
    //         ],
    //       ),
    //     ),
    //     hiSpace(width: 10),
    //     _ansButton(),
    //   ],
    // );
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Row(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Expanded(child: _showAns()),
            hiSpace(width: 8),
            _ansButton(),
          ],
        ),
        if (_showQzsj) hiSpace(height: 30),
        if (_showQzsj) _showQzsjItem(),
      ],
    );
  }

  /// 显示答案和已选择
  _showAns() {
    if (MyUtil.getDXT(widget.qType) ||
        MyUtil.getDXXZT(widget.qType) ||
        MyUtil.getPDT(widget.qType) ||
        MyUtil.getBDXXZT(widget.qType)) {
      return _showAnsXZT();
    } else if (MyUtil.getTKT(widget.qType)) {
      return _showAnsPDT();
    } else {
      return _showAnsWDT();
    }
  }

  /// 显示选择题的答案
  _showAnsXZT() {
    String ansV = widget.ans.join(" ");
    String selV = widget.sel.join(" ");
    if (MyUtil.getPDT(widget.qType)) {
      ansV = ansV == "1" ? "正确" : "错误";
      selV = selV == "1" ? "正确" : "错误";
    }

    return  Wrap(
      // mainAxisSize: MainAxisSize.min,
      children: [
        _showAnsTextDa(content: ansV),
        hiSpace(width: 15),
        if (widget.showSel) _showAnsTextNxz(tips: "你选择", content: selV),
      ],
    );
  }

  /// 显示答案字体
  _showAnsText({
    required String tips,
    required String content,
    required Color conColor,
  }) {
    return Text.rich(
      TextSpan(children: [
        TextSpan(
          text: "$tips ",
          style: FontUtil.fontBlack1(16, szYjms: szUtilComm.szInfo.yjms),
        ),
        TextSpan(
          text: content,
          style: TextStyle(
            fontSize: 16,
            fontWeight: FontWeight.w500,
            color: conColor,
          ),
        ),
      ]),
    );
  }

  /// 显示答案字体-答案
  _showAnsTextDa({required String content}) {
    return _showAnsText(
      tips: "答案",
      content: content,
      conColor: getColorBlue1(szYjms: szUtilComm.szInfo.yjms),
    );
  }

  /// 显示答案字体-你选择
  _showAnsTextNxz({required String tips, required String content}) {
    return _showAnsText(
      tips: tips,
      content: content,
      conColor: widget.suc
          ? getColorBlue1(szYjms: szUtilComm.szInfo.yjms)
          : getColorRed5(szYjms: szUtilComm.szInfo.yjms),
    );
  }

  /// 答案对应按钮
  _ansButton() {
    return GestureDetector(
      onTap: _onTapFunQzsj,
      child: Container(
        width: 120,
        padding: EdgeInsets.only(top: 4, bottom: 7),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.all(Radius.circular(24)),
          border: Border.all(
              color: getColorBlue1(szYjms: szUtilComm.szInfo.yjms), width: 1),
        ),
        child: Center(
          child: Text(
            "全站数据",
            style: FontUtil.fontBlue1(20, szYjms: szUtilComm.szInfo.yjms),
          ),
        ),
      ),
    );
  }

  /// 全站数据点击方法
  _onTapFunQzsj() {
    debugPrint("stemAnswerWidget 组件 -> 你点击了全站数据 ===> $_showQzsj");
    setState(() {
      _showQzsj = !_showQzsj;
    });
  }

  /// 显示
  _showAnsPDT() {
    String ansV = _getStrPDT(widget.ans);
    String selV = _getStrPDT(widget.sel);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _showAnsTextDa(content: ansV),
        if (widget.showSel) hiSpace(height: 20),
        if (widget.showSel) _showAnsTextNxz(tips: "你填入", content: selV),
      ],
    );
  }

  /// 将数据转换为固定格式
  String _getStrPDT(List<String> list) {
    String str = "";
    for (int i = 0; i < list.length; i++) {
      str += "${i + 1}：${list[i]}";
      if (i != list.length - 1) {
        str += " ";
      }
    }
    return str;
  }

  /// 显示问答题的答案
  _showAnsWDT() {
    String ansV = widget.ans.join("");
    if (ansV == "") {
      ansV = "无";
    }
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          "主观题不做判断",
          style: FontUtil.fontBlue1(20, szYjms: szUtilComm.szInfo.yjms),
        ),
        hiSpace(height: 8),
        _showAnsTextWDT(content: ansV),
      ],
    );
  }

  /// 问答题答案显示
  _showAnsTextWDT({required String content}) {
    String con = "<span style='color: #1E2023'>答案 </span> $content";
    return StemRichTextWidget(
      text: con,
      textStyle: FontUtil.fontBlue1(
        30,
        fontWeight: FontWeight.w500,
        szYjms: szUtilComm.szInfo.yjms,
      ),
    );
  }

  /// 显示全站数据内容方法
  _showQzsjItem() {
    return Text(
      "全站数据：本题共被做${widget.doingNumber}次，做错${widget.errorNumber}次",
      style: FontUtil.fontBlack1(20, szYjms: szUtilComm.szInfo.yjms),
    );
  }
}
